package cn.wjx.taobaounion.ui.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.wjx.taobaounion.model.domain.Categories;
import cn.wjx.taobaounion.ui.fragment.HomePagerFragment;

/**
 * @author WuChangJian
 * @date 2020/6/17 13:31
 */
public class HomePagerAdapter extends FragmentPagerAdapter {

    private List<Categories.DataBean> mCategoriesList = new ArrayList<>();
    public HomePagerAdapter(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return HomePagerFragment.newInstance(mCategoriesList.get(position));
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mCategoriesList.get(position).getTitle();
    }

    @Override
    public int getCount() {
        return mCategoriesList.size();
    }

    public void setCategories(Categories categories) {
        mCategoriesList.clear();
        mCategoriesList.addAll(categories.getData());
        notifyDataSetChanged();
    }
}
